package com.copm.ifm.message.config;

import com.copm.ifm.message.constance.RabbitMqCommon;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author tp
 */
@Configuration
@Slf4j
public class RabbitMqConfig {

    @Bean
    public CustomExchange customExchange() {
        Map<String, Object> args = new HashMap<>(4);
        args.put("x-delayed-type", "direct");
        return new CustomExchange(RabbitMqCommon.DELAYED_EXCHANGE_NAME, "x-delayed-message", true, false, args);
    }

    @Bean
    public Queue immediateQueue() {
        return new Queue(RabbitMqCommon.DELAYED_QUEUE_NAME);
    }

    @Bean
    public Binding bindingNotify(@Qualifier("immediateQueue") Queue queue,
                                 @Qualifier("customExchange") CustomExchange customExchange) {
        return BindingBuilder.bind(queue).to(customExchange).with(RabbitMqCommon.DELAYED_ROUTING_KEY).noargs();
    }

}
